<?php

namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 督学内容
 */
class Supervision extends Model
{
    protected $name = "supervision";

    /**
     * 获取督学列表
     */
    public function getSupervisionList($user_id, $type)
    {
        $list = self::where(function ($query) use ($user_id, $type) {
            if ($type == 1) {
                # 待提交
                $query->where(function ($q) {
                    $q->where('student_content', 'eq', '')
                      ->whereOr('student_pic', 'eq', '');
                });
            }
            if ($type == 2) {
                # 已提交
                $query->where(function ($q) {
                    $q->where('student_content', 'neq', '')
                      ->whereOr('student_pic', 'neq', '');
                });
            }
            $query->where('user_id', $user_id);
        })
            ->field('id as supervision_id,supervision_content,teacher_content,teacher_pic,end_time')
            ->select();
        foreach ($list as &$v) {
            $v['is_end'] = $v['end_time'] > time() ? 1 : 0; #1=未截止,0=已截止
            # 判断是否批改
            if ($v['teacher_content'] != '' && $v['teacher_pic'] != '') {
                $v['is_correct'] = 1; #1=已批改,0=待批改
            } else {
                $v['is_correct'] = 0; #1=已批改,0=待批改
            }
        }
        return $list;
    }


    /**
     * 会员督学是否弹窗
     */
    public function getUserValidSupervision($user_id){
        $current_time = time();
        return Db::name('user_supervisor')->where('user_id', $user_id)
            ->where('end_time', '>', $current_time)  // 未过期的资源
            ->where('teacher_id', '>', 0)  // 有老师分配的
            ->select();
    }

    
    

}
